<?php
/**
 * Created by PhpStorm.
 * User: Duanwangye
 * Date: 18/09/21
 * Company:财联集惠
 */

namespace app\backstage\controller;

use app\model\FreshBanner as Model;
use app\common\service\Banner as BannerService;

class Banner extends Base
{
    //获取列表
    public function index() {

        $page = $this->request->post('page', 1);
        $size = $this->request->post('size', config('apiAdmin.ADMIN_LIST_DEFAULT'));
        $where = [];
        if( isset($this->request->post()['place']) ) {
            $where['place'] = $this->request->post()['place'];
        }
        if( isset($this->request->post()['title']) ) {
            $where['title'] = ['like','%'.$this->request->post()['title'].'%'];
        }
        $banner_model = new Model();
        $list = $banner_model
            ->limit(($page-1)*$size, $size)
            ->field('bannerID,title,thumb,sort,link,status,type,place,showDate,content,addTime,color')
            ->where($where)
            ->select();
        if ($list){
            $list->append(['statusText','typeText','placeText']);
        }

        foreach ($list as $k => $v){
            $list[$k]['showDate'] = explode(',',$v['showDate']);
        }
        $count = $banner_model ->where($where)->count();   //总条数

        return $this->buildSuccess([
            'count'=>$count,
            'list'=>$list
        ]);
    }

    /*
     * @name 更改状态
     * @param status
     * @param bannerID
     */
    public function setStatus(){

        $bannerID = $this->request->post('bannerID');
        $status = $this->request->post('status');
//        halt($status);
        $model = Model::where('bannerID', $bannerID)->update(['status' => $status]);
        if ($model){
            return $this->buildSuccess([]);
        }else{
            return $this->buildFailed(-2,'操作失败');
        }
    }

    //获取banner信息
    public function getinfo() {
        $model = new Model();
        $params_where['bannerID'] = $this->app['bannerID'];
        $info = $model
            ->field('bannerID,title,thumb,sort,link,status,type,place,showDate,color')
            ->find($params_where);
        return $this->buildSuccess($info);
    }

    //新增banner图片
    public function add() {
        $postData = $this->request->post();
        $params['title'] = $postData['title'];
        $params['thumb'] = $postData['thumb'];
        $params['sort'] = $postData['sort'];
        //$params['productID'] = $this->app['productID'];
        $params['link'] = $postData['link'];
        $params['status'] = $postData['status'];
        $params['type'] = $postData['type'];
        $params['place'] = $postData['place'];
        $params['showDate'] = implode(',',$postData['showDate']);
        $params['content'] = !empty($postData['content']) ? $postData['content'] : '';
        $params['color'] = !empty($postData['color']) ? $postData['color'] : '';
        $crate_result = Model::create($params);
        if ($crate_result){
            return $this->buildSuccess([]);
        }else{
            return $this->buildFailed(-1,'新增失败');
        }
    }

    //修改广告图
    public function edit(){
        $postData = $this->request->post();
        $params['title'] = $postData['title'];
        $params['thumb'] = $postData['thumb'];
        $params['sort'] = $postData['sort'];
        //$params['productID'] = $this->app['productID'];
        $params['link'] = $postData['link'];
        $params['status'] = $postData['status'];
        $params['type'] = $postData['type'];
        $params['place'] = $postData['place'];
        $params['color'] = $postData['color'];
        $params['content'] = !empty($postData['content']) ? $postData['content'] : '';
        $params['showDate'] = implode(',',$postData['showDate']);
        $params_where['bannerID'] = $postData['bannerID'];

        $save_result = BannerService::updateBanner($params, $postData['bannerID']);

        if ($save_result){
            return $this->buildSuccess([]);
        }else{
            return $this->buildFailed(-1,'更新失败');
        }
    }

    /*
     * @name 删除banner图
     * @param bannerIDS
     */
    public function delBanner(){
        //$this->app['bannerIDS'] = [20];
        $postData = $this->request->post();
        $res = Model::destroy($postData['bannerID']);
        if($res){
            return $this->buildSuccess([]);
        }else{
            return $this->buildFailed(-2,'操作失败');
        }
    }

}